import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { message } from 'antd';
import moment from 'moment';
import { DATA_ENTRY, AdvancedSearch, NoQueryC, ContentPackage } from '../../../comps';
import Brinson from './Brinson'; // 模型为Brinson展示的组件
import Campisi from './Campisi'; // 模型为Campisi展示的组件
import Barra from './Barra'; // 模型为Barra时展示的组件
import { isZiGuanEvir } from '../../../utils/commonFunc';
import { formSelectData } from '../../../utils/utils';

@connect(({ attributionAnalysisM, yieldPerformanceM, globalM }) => ({
  attributionAnalysisM,
  yieldPerformanceM,
  globalM,
}))
export default class AttributionAnalysis extends PureComponent {
  state = {
    productData: '',
    index: '', // 传入各组件的时间类型
    portfolioCode: '', // 传入各组件的产品code
    isShow: false, // 是否展示详细信息
    ModelValue: '', // 模型设置默认值
    setupModel: undefined, // 模型设置的值
    isModel: true, // 模型设置是否置灰
    modelSelectData: [], // 模型设置下拉数据
  };

  componentDidMount() {}

  // 搜索点击事件
  searchData = (e) => {
    const { dispatch } = this.props;
    if (e.product) {
      if (e.begin1 && e.begin2 && e.setModel) {
        dispatch({
          type: 'attributionAnalysisM/benchData',
          payload: {
            portfolioCode: this.state.productData, // 产品Id
            beginDate: e.begin1.format('YYYY-MM-DD'),
            endDate: e.begin2.format('YYYY-MM-DD'),
            setupModel: e.setModel,
          },
          callback: (res) => {
            if (res) {
              this.setState({
                index: e.complextdate1, // 时间类型
                portfolioCode: this.state.productData, // 产品Id
                beginDate: e.begin1.format('YYYY-MM-DD'),
                endDate: e.begin2.format('YYYY-MM-DD'),
                setupModel: e.setModel, // 模型设置
                isShow: true, // 判断是否展示页面的条件
              });
            }
          },
        });
      } else {
        message.warning('请填写完整的查询条件');
        this.setState({
          beginDate: undefined,
          endDate: undefined,
          setModel: undefined,
        });
      }
    } else {
      message.warning('请选择选择产品');
    }
  };

  // 产品改变事件
  changePortfolio = (e) => {
    if (e) {
      const data = e.info;
      const {
        dispatch,
        globalM: {
          dataDictionary: { ATTRIBUTION_MODEL = [], MAAT_PRODUCT_INVE_TYPE = [] },
        },
      } = this.props;
      const modelData = formSelectData(ATTRIBUTION_MODEL);
      // maat和资管 的资产类型字典不一致，所以单独请求字典获取枚举值根据value值判断
      const t = MAAT_PRODUCT_INVE_TYPE.filter((x) => x.code == data.inveType)[0]?.value;

      if (data.inveType && data.inveType !== null) {
        if (t == '权益类') {
          // 权益类 展示Brinson  Barra
          this.setState(
            {
              modelSelectData: [modelData[0], modelData[2]], // 默认展示下拉框内首条
              isModel: false,
            },
            () => {
              const { modelSelectData } = this.state;
              this.ModelSet.props.form.setFieldsValue({
                setModel: modelSelectData.length ? modelSelectData[0].value : '', // 产品切换时重新设置模型设置默认值
              });
            },
          );
        } else if (t == '固收类') {
          // 固收类 展示 Campisi
          this.setState(
            {
              modelSelectData: [modelData[1]],
              isModel: false,
            },
            () => {
              const { modelSelectData } = this.state;
              this.ModelSet.props.form.setFieldsValue({
                setModel: modelSelectData.length ? modelSelectData[0].value : '', // 产品切换时重新设置模型设置默认值
              });
            },
          );
        } else {
          // 其他类型
          message.warning('暂不支持该产品的投资类型');
          this.setState(
            {
              modelSelectData: [],
              isModel: true,
            },
            () => {
              this.ModelSet.props.form.setFieldsValue({
                setModel: '', // 清空模型设置默认值
              });
            },
          );
        }
      } else {
        if (isZiGuanEvir()) {
          message.warning('该产品未设置投资类型，请至“产品基本信息”模块进行设置');
        } else {
          message.warning('该产品未设置投资类型，请至“产品概览”模块进行设置');
        }
        this.setState(
          {
            modelSelectData: [],
            isModel: true,
          },
          () => {
            this.ModelSet.props.form.setFieldsValue({
              setModel: '', // 没有投资类型是清空模型设置默认值
            });
          },
        );
      }

      this.setState({
        productData: data.portfolioCode,
        portfolioName: data.portfolioName,
      });
    } else {
      this.ModelSet.props.form.setFieldsValue({
        setModel: '', // 产品切换时重新设置模型设置默认值
      });
      this.setState({
        productData: '',
        isModel: true,
      });
    }
  };

  // 根据模型类别展示不同页面
  showContent = () => {
    const { portfolioCode, setupModel } = this.state;
    const args = {
      portfolioCode,
      timeRange: this.state.index,
      beginDate: this.state.beginDate,
      endDate: this.state.endDate,
      setupModel: this.state.setupModel,
    };
    switch (
      setupModel // 根据不同的模型展示不同的页面
    ) {
      case '0':
        return <Brinson args={args} />;
      case '1':
        return <Campisi args={args} />;
      default:
        return <Barra args={args} />;
    }
  };

  render() {
    const { modelSelectData, isShow } = this.state;
    const searchProps = {
      dataSource: [
        {
          dataIndex: 'product',
          title: '产品',
          type: DATA_ENTRY.PRODUCTLIST,
          dataType: 2,
          onChange: (e) => this.changePortfolio(e),
          tradeModel: ['BOOKKEEPING_MODE', 'COMMAND_MODE', 'VALUATION_BOOKKEEPING_MODEL'],
        },
        {
          selectIndex: 'complextdate1',
          startIndex: 'begin1',
          endIndex: 'begin2',
          title: '时间区间',
          type: DATA_ENTRY.HOlDDATERANGEC,
          productCode: this.state.productData,
          flag: '2',
          defaultType: '2',
        },
        {
          dataIndex: 'setModel',
          title: '模型设置',
          initialValue: modelSelectData.length ? modelSelectData[0].value : '',
          type: DATA_ENTRY.SELECT,
          dataSource: modelSelectData,
          form: this.props.form,
          disabled: this.state.isModel,
          onRef: (ref) => {
            this.ModelSet = ref;
          },
        },
      ],
      onQuery: (e) => {
        this.searchData(e);
      },
      onClear: () => {
        const { form } = this.props;
        form?.resetFields(); // 清空表单
        this.setState({
          isModel: true, // 模型设置置灰
          modelSelectData: [], // 模型设置下拉选项清空
          productData: '',
        });
      },
    };
    return (
      <ContentPackage
        title='归因分析'
        contentHtml={
          <div style={{ padding: '0 15px' }}>
            <div>
              <AdvancedSearch {...searchProps} />
            </div>
            {isShow ? (
              <>{this.showContent()} </> // 满足条件时展示页面
            ) : (
              <div style={{ marginTop: '20px' }}>
                <NoQueryC />
              </div>
            )}
          </div>
        }
      />
    );
  }
}
